package com.jwdf.uda.view.prompt;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ViewPromptQuestionBinding;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.PromptUtil;
import com.jwdf.uda.view.dialog.BaseDialog;

/**
 * Created by Administrator on 2017/1/13.
 */
public class QuestionPrompt extends BaseDialog implements DialogInterface.OnDismissListener {
    private ViewPromptQuestionBinding mBinding;
    private View mNickView;
    private View mDescribeView;
    private View mProblemsView;
    private View mVoteView;
    private int deviceWidth;
    private boolean mCancelable;

    public QuestionPrompt(Context context, View guideView ,View describeView ,View problemsView ,View voteView) {
        super(context);
        this.mNickView = guideView;
        this.mDescribeView=describeView;
        this.mProblemsView=problemsView;
        this.mVoteView=voteView;
        this.deviceWidth = DisplayUtils.getDeviceWidth(context);
    }

    public QuestionPrompt(Context context, View guideView ,View describeView ,View problemsView , View voteView , boolean cancelable) {
        this(context, guideView,describeView,problemsView,voteView);
        mCancelable = cancelable;
    }

    private Handler handler = new Handler();

    private Runnable mNickRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mNickView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mNickView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.ivNickTxt);
//            location[1] -=viewHeight;
            location[1] -=viewTextHeight;
            viewHeight+=DisplayUtils.dip2px(getContext(), 12);
            deviceWidth-=DisplayUtils.dip2px(getContext(), 28);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(deviceWidth, viewHeight);
            params.addRule(RelativeLayout.BELOW, mBinding.ivNickTxt.getId());
            params.leftMargin=DisplayUtils.dip2px(getContext(), 14);
            mBinding.ivNick.setLayoutParams(params);

            mBinding.ivNick.setVisibility(View.VISIBLE);
            mBinding.ivNickTxt.setVisibility(View.VISIBLE);
//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            mBinding.ivNickTxt.setPadding(DisplayUtils.dip2px(getContext(), 0), location[1] - DisplayUtils.dip2px(getContext(), 6), 0, DisplayUtils.dip2px(getContext(), 0));
        }
    };



    private Runnable mDescribeRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mDescribeView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mDescribeView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.ivDescribeTxt);
//            location[1] -=viewHeight;
            location[1] -=viewTextHeight;
            viewHeight-=DisplayUtils.dip2px(getContext(), 50);
//            deviceWidth-=DisplayUtils.dip2px(getContext(), 20);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(deviceWidth, viewHeight);
            params.addRule(RelativeLayout.BELOW, mBinding.ivDescribeTxt.getId());
            params.leftMargin=DisplayUtils.dip2px(getContext(), 14);
            mBinding.ivDescribe.setLayoutParams(params);

            mBinding.ivDescribe.setVisibility(View.VISIBLE);
            mBinding.ivDescribeTxt.setVisibility(View.VISIBLE);
//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            mBinding.ivDescribeTxt.setPadding(DisplayUtils.dip2px(getContext(), 0), location[1] - DisplayUtils.dip2px(getContext(), 9), 0, DisplayUtils.dip2px(getContext(), 3));
        }
    };


    private Runnable mProblemsRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mProblemsView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mProblemsView);
            int viewLeft=PromptUtil.getMeasuredWidth(mProblemsView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.ivProblemsTxt);
            location[1] -=DisplayUtils.dip2px(getContext(), 4);
            location[0]-=DisplayUtils.dip2px(getContext(), 4);
            viewHeight+=DisplayUtils.dip2px(getContext(), 8);
            viewLeft+=DisplayUtils.dip2px(getContext(), 8);
//            deviceWidth-=DisplayUtils.dip2px(getContext(), 20);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( viewLeft, viewHeight);
//            params.addRule(RelativeLayout.LEFT_OF, mBinding.ivProblemsTxt.getId());

            params.topMargin=location[1];
            params.leftMargin=location[0];
            params.bottomMargin=0;
            params.rightMargin=0;
            mBinding.ivProblems.setLayoutParams(params);

            mBinding.ivProblems.setVisibility(View.VISIBLE);
            mBinding.ivProblemsTxt.setVisibility(View.VISIBLE);
//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            mBinding.ivProblemsTxt.setPadding(viewLeft-DisplayUtils.dip2px(getContext(), 8), location[1] - DisplayUtils.dip2px(getContext(), 4), 0, DisplayUtils.dip2px(getContext(), 0));
        }
    };


    private Runnable mVoteRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mVoteView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mVoteView);
            int viewLeft=PromptUtil.getMeasuredWidth(mVoteView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.rlVote);
            location[1] -=DisplayUtils.dip2px(getContext(), 4);
            location[0]-=DisplayUtils.dip2px(getContext(), 4);
            viewHeight+=DisplayUtils.dip2px(getContext(), 8);
            viewLeft+=DisplayUtils.dip2px(getContext(), 8);
//            deviceWidth-=DisplayUtils.dip2px(getContext(), 20);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( viewLeft, viewHeight);
//            params.addRule(RelativeLayout.LEFT_OF, mBinding.ivProblemsTxt.getId());
            params.topMargin=location[1];
            params.leftMargin=location[0];
            params.bottomMargin=0;
            params.rightMargin=0;
            mBinding.rlVote.setLayoutParams(params);

            mBinding.rlVote.setVisibility(View.VISIBLE);
            mBinding.ivVoteTxt.setVisibility(View.VISIBLE);
//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            mBinding.ivVoteTxt.setPadding(0, location[1]+viewTextHeight+DisplayUtils.dip2px(getContext(), 8), 0, DisplayUtils.dip2px(getContext(), 0));
        }
    };


    private Runnable mBtnRunnable = new Runnable() {
        @Override
        public void run() {
//            mBinding.ivBtn.setVisibility(View.VISIBLE);
//            mBinding.ivBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dismiss();
//                }
//            });
            mBinding.rlQuestionClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    };

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_prompt_question, null, false);
        mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (View.VISIBLE == mBinding.ivBtn.getVisibility()) {
                    dismiss();
                }
            }
        });
        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        setOnDismissListener(this);
        handler.postDelayed(mNickRunnable, 500);
        handler.postDelayed(mDescribeRunnable, 1000);
        handler.postDelayed(mProblemsRunnable, 1500);
        handler.postDelayed(mVoteRunnable,2000);
        handler.postDelayed(mBtnRunnable, 2500);
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(mNickRunnable);
        handler.removeCallbacks(mBtnRunnable);
        handler.removeCallbacks(mProblemsRunnable);
        handler.removeCallbacks(mDescribeRunnable);
        handler.removeCallbacks(mVoteRunnable);
        PromptUtil.setHideQuestionPrompt(getContext());
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
            dismiss();
        }
    }
}
